package jaran.poem.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class jdbcpoem1203 {
    public static Connection get() throws Exception{
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/test?useSSL=false";
        String username="root";
        String password="root";
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }
    public static int updata(String sql,Connection connection) throws Exception{
        Statement statement=connection.createStatement();
        int i=statement.executeUpdate(sql);
        if (i>0){
            return 1;
        }
        else return 0;
    }
    public static void select(String sql,Connection connection)throws Exception{
        Statement statement=connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            long id = resultSet.getLong(1);
            String name = resultSet.getString(2);
            System.out.println("id=" + id + ", name=" + name);
        }

    }

    public static void main(String[] args) throws Exception{
        Connection connection=get();
        String newname,newpass;
        newname = "new";
        newpass = "000";
        String sql1="insert into `user` (username,password) values ('" +newname+
                "',MD5('" +newpass+
                "')) ";
        int i=updata(sql1,connection);
        System.out.println(i);
        String sql2="select * from `user`";
        select(sql2,connection);

    }
}